package com.morimoto.rmlib;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;

/**
 * 
 * @author Ryohei Morimoto
 *
 */
public class DrawElement {

	private Bitmap mBitmap = null;
	private RectF mRect = null;
	private boolean mIsShowing = true;
	private Paint mPaint = null;
	private int mAlpha = 255;
	
	public DrawElement() {
		mRect = new RectF();
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
	}
	
	public void init(Bitmap bitmap, float cx, float cy, float w, float h) {
		mBitmap = bitmap;
		mRect.set(cx - w / 2.0f, cy - h / 2.0f, cx + w / 2.0f, cy + h / 2.0f);
	}

	public void draw(Canvas canvas) {
		if (mBitmap != null && mRect != null && mIsShowing) {
			canvas.drawBitmap(mBitmap, null, mRect, mPaint);
		}
	}
	
	public void setBitmap(Bitmap bitmap) {
		mBitmap = bitmap;
	}
	
	public void setSize(float w, float h) {
		final float cx = mRect.centerX();
		final float cy = mRect.centerY();
		mRect.set(cx - w / 2.0f, cy - h / 2.0f, cx + w / 2.0f, cy + h / 2.0f);
	}
	
	public void postScale(float ds) {
		final float cx = mRect.centerX();
		final float cy = mRect.centerY();
		final float w = mRect.width() * ds;
		final float h = mRect.height() * ds;
		mRect.set((cx - w / 2.0f), cy - h / 2.0f, cx + w / 2.0f, cy + h / 2.0f);
	}
	
	public void setCenterXY(float cx, float cy) {
		final float w = mRect.width();
		final float h = mRect.height();
		mRect.set(cx - w / 2.0f, cy - h / 2.0f, cx + w / 2.0f, cy + h / 2.0f);
	}
	
	public void postCenterXY(float dx, float dy) {
		final float cx = mRect.centerX() + dx;
		final float cy = mRect.centerY() + dy;
		final float w = mRect.width();
		final float h = mRect.height();
		mRect.set(cx - w / 2.0f, cy - h / 2.0f, cx + w / 2.0f, cy + h / 2.0f);
	}
	
	public void setAlpha(int alpha) {
		mAlpha = alpha;
		mPaint.setAlpha(Math.round(alpha));
	}
	
	public int getAlpha() {
		return mAlpha;
	}
	
	public boolean hitTestRect(float x, float y) {
		return (x < mRect.right && x > mRect.left && y > mRect.top && y < mRect.bottom) ? true : false;
	}

	public boolean hitTestRectOffset(float x, float y, float offsetX, float offsetY) {
		return (x < mRect.right + offsetX && x > mRect.left - offsetX 
				&& y > mRect.top - offsetY && y < mRect.bottom + offsetY) ? true : false;
	}

	public boolean hitTestCircle(float x, float y){
		final float cx = mRect.centerX();
		final float cy = mRect.centerY();
		float radius = Math.abs((mRect.top - mRect.bottom) / 2.0f);
		return (((cx - x) * (cx - x) + (cy - y) * (cy - y)) < radius * radius) ? true : false;
	}

	public boolean hitTestCircleOffset(float x, float y, float offsetRadius){
		final float cx = mRect.centerX();
		final float cy = mRect.centerY();
		float radius = Math.abs(offsetRadius + (mRect.top - mRect.bottom) / 2.0f);
		return (((cx - x) * (cx - x) + (cy - y) * (cy - y)) < radius * radius) ? true : false;
	}
	
	public void setVisibility(boolean isVisible) {
		mIsShowing = isVisible;
	}

	public boolean getVisibility() {
		return mIsShowing;
	}

	public void setRectF(RectF rect) {
		mRect = rect;
	}
	
	public RectF getRectF() {
		return mRect;
	}
	
	public float getCenterX() {
		return mRect.centerX();
	}

	public float getCenterY() {
		return mRect.centerY();
	}

	public float getWidth() {
		return mRect.centerX();
	}

	public float getHeight() {
		return mRect.centerY();
	}
}
